package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.dmsdbj.itoo.tool.tenancy.TenancyContext;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.StudentTeachClassEntity;
import com.tfjybj.itoo.teach.model.StudentModel;
import com.tfjybj.itoo.teach.model.StudentTeachClassModel;
import com.tfjybj.itoo.teach.provider.service.StudentService;
import com.tfjybj.itoo.teach.provider.service.StudentTeachClassService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * StudentTeachClassController
 * studentTeachClass表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"studentTeachClass表接口"})
@RequestMapping(value = "/studentTeachClass")
@RestController
@Slf4j
public class StudentTeachClassController {

    @Resource
    private StudentTeachClassService studentTeachClassService;

    @Resource
    private StudentService studentService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model StudentTeachClassModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody StudentTeachClassModel model) {
        if (StringUtils.isEmpty(model.getStudentId())) {
            return ItooResult.build(ItooResult.FAIL, "studentId为空");
        }
        if (StringUtils.isEmpty(model.getTeachClassId())) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId为空");
        }
        //校验学生是否重复添加
        Boolean studentcount= studentTeachClassService.checkByStudentCode(model.getStudentId());
        if(studentcount){
            return ItooResult.build(ItooResult.FAIL, "添加失败，此学生已经添加！");
        }
        StudentTeachClassEntity studentTeachClassEntity = new StudentTeachClassEntity();
        BeanUtils.copyProperties(model, studentTeachClassEntity);
        studentTeachClassService.save(studentTeachClassEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        studentTeachClassService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        studentTeachClassService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }


    /**
     * 修改
     *
     * @param model StudentTeachClassModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改studentTeachClass")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody StudentTeachClassModel model) {
        if (StringUtils.isEmpty(model.getStudentId())) {
            return ItooResult.build(ItooResult.FAIL, "studentId为空");
        }
        if (StringUtils.isEmpty(model.getTeachClassId())) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId为空");
        }
        StudentTeachClassEntity studentTeachClassEntity = new StudentTeachClassEntity();
        BeanUtils.copyProperties(model, studentTeachClassEntity);
        studentTeachClassService.updateById(studentTeachClassEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找StudentTeachClass
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        StudentTeachClassEntity studentTeachClassEntity = studentTeachClassService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentTeachClassEntity);
    }
   

    /**
     * 分页查询所有StudentTeachClass
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有StudentTeachClass")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StudentTeachClassEntity> studentTeachClasss = studentTeachClassService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentTeachClasss);
    }

    /**
     * 添加
     *
     * @param model StudentTeachClassModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/createBystudentid"})
    public ItooResult createBystudentid(@RequestBody StudentTeachClassModel model) {
        if (StringUtils.isEmpty(model.getStudentId())) {
            return ItooResult.build(ItooResult.FAIL, "studentId为空");
        }
        if (StringUtils.isEmpty(model.getTeachClassId())) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId为空");
        }
        boolean flag= studentTeachClassService.createBystudentid(model.getStudentId(),model.getTeachClassId());
        return ItooResult.build(ItooResult.SUCCESS, "添加成功",flag);
    }

    /**
     * 根据学生_id模糊查询studentTeachClass
     *
     * @param studentId 学生_id
     * @param pageNo    页码
     * @param pageSize  页数
     * @return 模糊查询的studentTeachClass
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据学生_id模糊查询studentTeachClass", notes = "分页根据学生_id模糊查询studentTeachClass")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "queryStudentTeachClassByLikeStudentId/{pageNo}/{pageSize}")
    public ItooResult queryStudentTeachClassByLikeStudentId(@RequestParam(required = false, defaultValue = "") String studentId,
                                                            @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<StudentTeachClassEntity> studentTeachClassList = studentTeachClassService.queryByLikeStudentId(studentId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentTeachClassList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */
 /**
     * 根据学生id查找上课班Ids
     *
     * @param studentId 学生id
     * @author 严文文
     * @since 2.0.0 2018-11-18 16:17:20
     */
    @ApiOperation(value = "根据学生id查找上课班Ids")
    @GetMapping(value = {"/findTeachClassIdsByStudentId/{studentId}"})
    public ItooResult findTeachClassIdsByStudentId(@ApiParam(value = "学生id", required = true) @PathVariable String studentId) {
        List<String> teachClassIds = studentTeachClassService.findTeachClassIdsByStudentId(studentId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teachClassIds);
    }
	
    /**
     * 根据学生ID批量删除上课班对应的学生信息
     *
     * @param studentIds   学生ID
     * @param teachClassId 上课班ID
     * @return ItooResult
     * @author 严文文
     * @since 2.0.0 2 2018-11-14 09:37:38
     */
    @ApiOperation(value = "根据学生ID批量删除上课班对应的学生信息")
    @DeleteMapping(value = {"/deleteStudentTeachClass/{teachClassId}"})
    public ItooResult deleteAdmTeachclass(@ApiParam(value = "学生Ids", required = true) @RequestBody List<String> studentIds,
                                          @ApiParam(value = "上课班Id", required = true) @PathVariable String teachClassId) {

        String operator = TenancyContext.UserID.get();
        int result;
        if (StringUtils.isEmpty(studentIds)) {
            return ItooResult.build(ItooResult.FAIL, "studentIds");
        }
        if (StringUtils.isEmpty(operator)) {
            return ItooResult.build(ItooResult.FAIL, "operator");
        }
        if (StringUtils.isEmpty(teachClassId)) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId");
        }
        result = studentTeachClassService.deleteStudentTeachClass(studentIds, teachClassId, operator);
        return ItooResult.build(ItooResult.SUCCESS, "对应的学生信息", result);
    }

    /**
     * 根据学生ID单个删除上课班对应的学生信息
     * @param studentId
     * @param teachClassId
     * @return
     * @author 严文文
     * @since 2.0.0 2 2018-12-5 16:17:07
     */
    @ApiOperation(value = "根据学生ID单个删除上课班对应的学生信息")
    @DeleteMapping(value = {"/deleteSingleStudentTeachClass/{teachClassId}/{studentId}"})
    public ItooResult deleteSingleStudentTeachClass(@ApiParam(value = "学生Id", required = true) @PathVariable String studentId,
                                                    @ApiParam(value = "上课班Id", required = true) @PathVariable String teachClassId) {

        String operator = TenancyContext.UserID.get();
        boolean  result;
        if (StringUtils.isEmpty(studentId)) {
            return ItooResult.build(ItooResult.FAIL, "studentId");
        }
        if (StringUtils.isEmpty(operator)) {
            return ItooResult.build(ItooResult.FAIL, "operator");
        }
        if (StringUtils.isEmpty(teachClassId)) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId");
        }
        result = studentTeachClassService.deleteSingleStudentTeachClass(studentId, teachClassId, operator);
        return ItooResult.build(ItooResult.SUCCESS, "对应的学生信息", result);
    }




    /**
     * 根据学生id查找上课班CourseID
     *
     * @param studentId 学生id
     * @author 胡泽宽
     * @since 2.0.0 2018年11月19日
     */
    @ApiOperation(value = "根据学生id查找上课班CourseID")
    @GetMapping(value = {"/querycourseIDBystudentid/{studentId}"})
    public ItooResult querycourseIDBystudentid(@ApiParam(value = "学生id", required = true) @PathVariable String studentId) {
        List<String> courseIds = studentTeachClassService.querycourseIDBystudentid(studentId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseIds);
    }

    
    /**
     * 根据学生ID和上课班id删除上课班对应的学生信息
     *
     * @param model   学生ID,上课班id
     * @return ItooResult
     * @author 胡泽宽
     * @since 2.0.0 2 2018年11月24日 20:44:39
     */
    @ApiOperation(value = "根据学生ID删除上课班对应的学生信息")
    @DeleteMapping(value = {"/deleteStudentTeachClassbyId"})
    public ItooResult deleteStudentTeachClassbyId(@RequestBody StudentTeachClassModel model) {

        String operator = TenancyContext.UserID.get();

        if (StringUtils.isEmpty(model.getStudentId())) {
            return ItooResult.build(ItooResult.FAIL, "studentIds");
        }
        if (StringUtils.isEmpty(operator)) {
            return ItooResult.build(ItooResult.FAIL, "operator");
        }
        if (StringUtils.isEmpty(model.getTeachClassId())) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId");
        }
        boolean flag;
        flag = studentTeachClassService.deleteStudentTeachClassbyId(model.getStudentId(),model.getTeachClassId());
        return ItooResult.build(ItooResult.SUCCESS, "删除的学生信息", flag);
    }

 /**
     * 分页根据关键词模糊查询某上课班下的学生姓名
     *
     * @param strLike
     * @param teachClassId
     * @param pageNum
     * @param pageSize
     * @return
     * @author 严文文
     * @since 2.0.0 2018-11-23 20:56:40
     */
    @ApiOperation(value = "分页根据关键词模糊查询某上课班下的学生姓名")
    @GetMapping(value = "queryTeachClassStudentInfoByNameStrLike/{pageNum}/{pageSize}")
    public ItooResult queryTeachClassStudentInfoByNameStrLike(
            @RequestParam(required = false, defaultValue = " ") String strLike,
            @RequestParam(required = false, defaultValue = " ") String teachClassId,
            @ApiParam(name = "pageNum", value = "页码", required = true, example = "1") @PathVariable Integer pageNum,
            @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StudentModel> studentModelPageInfo  = studentTeachClassService.queryTeachClassStudentInfoByNameStrLike(strLike,teachClassId,pageNum,pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentModelPageInfo);
    }

    /**
     * 分页根据关键词模糊查询某上课班下的学生姓名,学号
     *
     * @param studentName
     * @param studentCode
     * @param teachClassId
     * @param pageNum
     * @param pageSize
     * @return
     * @author 严文文
     * @since 2.0.0 2018-11-24 08:59:38
     */
    @ApiOperation(value = "分页根据关键词模糊查询某上课班下的学生姓名,学号")
    @GetMapping(value = "queryTeachClassStudentInfoByNameAndCodeStrLike/{pageNum}/{pageSize}")
    public ItooResult queryTeachClassStudentInfoByNameAndCodeStrLike(
            @RequestParam(required = false, defaultValue = " ") String studentName,
            @RequestParam(required = false, defaultValue = " ") String studentCode,
            @RequestParam(required = false, defaultValue = " ") String teachClassId,
            @ApiParam(name = "pageNum", value = "页码", required = true, example = "1") @PathVariable Integer pageNum,
            @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<StudentModel> studentModelPageInfo  = studentTeachClassService.queryTeachClassStudentInfoByNameAndCodeStrLike(studentName,studentCode,teachClassId,pageNum,pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentModelPageInfo);
    }

    /**
     * 下载上课班-学生管理表模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 严文文
     * @since 2.0.0 2018-11-25 18:14:52
     */
    @ApiOperation(value = "下载上课班-学生管理表模板，批量导入使用", notes = "下载上课班-学生管理表模板，批量导入使用")
    @GetMapping(value = {"/downLoadStudentTeachClassTemplate"})
    public ItooResult downLoadStudentTeachClassTemplate(HttpServletResponse response) {
        if (studentTeachClassService.downLoadStudentTeachClassTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载上课班-学生管理表模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载上课班-学生管理表模板失败");
        }
    }

    /**
     * 批量导上课班-学生管理表
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 严文文
     * @since 2.0.0 2018-11-27 10:58:03
     */
    @ApiOperation(value = "批量导入上课班-学生管理表", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importStudentTeachClassByTemplate{teachClassId}"})
    public ItooResult importStudentTeachClassByTemplate(
            @RequestParam(value = "file") MultipartFile multipartFile,
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "上课班id", required = true) @PathVariable String teachClassId) {
        return studentTeachClassService.importStudentTeachClassByTemplate(multipartFile, request, response,teachClassId);
    }

    /**
     * 导出-导入失败的数据
     *
     * @param response  响应
     * @param errorListId 为成功导入的分组id
     * @return 课程信息model
     * @author 严文文
     * @since 2018-12-3 10:27:26
     */
    @ApiOperation(value = "导出-导入失败的数据")
    @GetMapping(value = "exportFile/downErrorList/{errorListId}")
    public ItooResult downErrorList(HttpServletResponse response,
                                    @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {

        if (studentTeachClassService.downErrorList(errorListId,response)){
            return ItooResult.build(ItooResult.SUCCESS,"导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL,"导出失败");
        }
    }

    /**
     * 添加上课班学生
     * @param model
     * @return
     * @author 严文文
     * @since 2.0.0 2018-12-3 17:20:18
     */
    @ApiOperation(value = "添加上课班学生")
    @PostMapping(value = "insertStudentTeachClass")
    public ItooResult insertStudentTeachClass(@RequestBody StudentTeachClassModel model){
        String studentId= studentService.queryStudentByCode(model.getStudentCode()).get(0).getId();
        model.setStudentId(studentId);
        return this.create(model);
    }


    /**
     * 添加学生信息根据学生id和上课班集合
     *
     * @param model StudentTeachClassModel
     * @return 添加的结果
     * @author 严文文
     * @since 2.0.0 2018-12-8 10:05:42
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/createByStudentidAndTeachClassIds"})
    public ItooResult createByStudentidAndTeachClassIds(@RequestBody StudentTeachClassModel model) {
        if (StringUtils.isEmpty(model.getStudentId())) {
            return ItooResult.build(ItooResult.FAIL, "studentId为空");
        }
        if (StringUtils.isEmpty(model.getTeachClassIds())) {
            return ItooResult.build(ItooResult.FAIL, "teachClassIds为空");
        }
        boolean flag= studentTeachClassService.createByStudentidAndTeachClassIds(model.getStudentId(),model.getTeachClassIds());
        return ItooResult.build(ItooResult.SUCCESS, "添加成功",flag);
    }


    /**
     * 根据上课班id查询学生信息
     * @param teachClassId
     * @return 学生信息list
     * @author 郝雨烁
     * @since 0.0.1 2019年3月6日16:34:48
     */
    @ApiOperation(value = "根据上课班id查询学生信息")
    @GetMapping(value = {"/queryStudents/{teachClassId}"})
    public ItooResult queryStudents(@ApiParam(value = "上课班id", required = true)@PathVariable String teachClassId){
        if(StringUtils.isEmpty(teachClassId)){
            log.warn("上课班id不能为空");
            return ItooResult.build(ItooResult.FAIL,"查询失败");
        }
        return ItooResult.build(ItooResult.SUCCESS,"查询成功",studentTeachClassService.queryStudents(teachClassId));
    }

    /**
     * 根据上课班id和学生id删除这个学生
     * @param teachClassId
     * @param studentId
     * @return
     * @author 郝雨烁
     * @since 0.0.1 2019年3月7日21:32:17
     */
    @ApiOperation(value = "根据上课班id和学生id删除这个学生")
    @PostMapping(value = {"/deleteStudentInGroups/{teachClassId}/{studentId}"})
    public ItooResult deleteStudentInGroups(@ApiParam(value = "上课班id",required = true)@PathVariable String teachClassId,
                                            @ApiParam(value = "学生id",required = true)@PathVariable String studentId){
        if(StringUtils.isEmpty(teachClassId)){
            log.warn("上课班id不能为空");
            return ItooResult.build(ItooResult.FAIL,"查询失败");
        }
        if(StringUtils.isEmpty(studentId)){
            log.warn("学生id不能为空");
            return ItooResult.build(ItooResult.FAIL,"查询失败");
        }
       boolean flag =  studentTeachClassService.deleteStudentInGroups(teachClassId,studentId);
        if(flag == true){
            return ItooResult.build(ItooResult.SUCCESS,"删除成功");
        }else{
            return ItooResult.build(ItooResult.FAIL,"没有要删除的数据");
        }

    }
}    
